package my.wealth;

import java.util.*;

public class Wealth {
    public  static final Map<Integer, Integer> map=new HashMap<>();

    public static int people=100;

    public Wealth() {

        for (int i = 0; i < people; i++) {
            //每人初始值100
            map.put(i,100);
        }
    }
    //获取随机数
    public static Integer random(){
        int v = (int)(Math.random() * people);
        return v;
    }
    public static void transfer(){
        for (int i = 0; i < people; i++) {
            Integer j = map.get(i);
            if(j==0){continue;}
            //不能是自己
            Integer r=random();
            while (r==i){
                r=random();
            }
            //给别人前先减一
            map.put(i,j-1);
            //其他人收
            map.put(r,map.get(r)+1);
        }
    }
    public static void main(String[] args) {
        Wealth wealth = new Wealth();
        //进行10000次
        for (int i = 0; i < 10000; i++) {
            transfer();
        }
        Set<Integer> set = map.keySet();
        Integer total=0;
        List<Integer> list=new ArrayList<>();
        int low100=0;
        int low50=0;
        for (Integer i : set) {
            //   System.out.println(i+"-"+map.get(i));
            list.add(map.get(i));
            total+=map.get(i);
            if(map.get(i)<100){
                low100++;
            }
            if(map.get(i)<50){
                low50++;
            }
        }
        //计算总数
        System.out.println("总数是："+total);
        //统计
        Collections.sort(list);
        System.out.println("排序后："+list);
        //前10%
        int i10 = people-people*10/100;
        int t10=0;
        for (int i = i10; i < people; i++) {
            Integer i1 = list.get(i10);
            t10+=i1;
        }
        System.out.println("前10%总共："+t10);
        //前20%
        int i20 = people-people * 20/100;
        int t20=0;
        for (int i = i20; i <people; i++) {
            Integer i1 = list.get(i20);
            t20+=i1;
        }
        System.out.println("前20%总共："+t20);
        //低于100的人数
        System.out.println("低于100的人数："+low100);
        //低于50的人数
        System.out.println("低于50的人数："+low50);
    }
}
